#include "CItemMgr.h"
#include "pb_cfg_item.pb.h"
#include "commerrno.h"
#include <iostream>
#include <fstream>
#include <vector>

#include "MsgComm.pb.h"
#include "../cmdinner/CCmdDropItem.h"
#include "../mainctrl.h"
#include "../equip/CEquipMgr.h"
#include "../instance/CInstanceMgr.h"
#include "commmsg.h"
#include "CTRandomNum.h"
#include "../card/CCardMgr.h"
#include "../storage/CStorageMgr.h"
#include "../oss/COssMgr.h"

int CItemMgr::Init(const char * sBuff)
{
    int iRet = 0;
    int iOffset = 0;

    iRet = _stMaterialConfig.Init(sBuff + iOffset);
    CHECK_RETURN(iRet);
    iOffset += CMaterialConfig::CountSize();

    iRet = _stScrollConfig.Init(sBuff + iOffset);
    CHECK_RETURN(iRet);
    iOffset += CScrollConfig::CountSize();

    iRet = _stCrystalConfig.Init(sBuff + iOffset);
    CHECK_RETURN(iRet);
    iOffset += CCrystalConfig::CountSize();

    iRet = _stJewelConfig.Init(sBuff +iOffset);
    CHECK_RETURN(iRet);
    iOffset += CJewlConfig::CountSize();

    return 0;
}

int CItemMgr::GetScrollConfig(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey,
                              CScrollConfigData& stConfigData)
{
    return _stScrollConfig.GetConfig(szKey, stConfigData);
}

int CItemMgr::GetScrollConfig(unsigned short usScrollConfigID, CScrollConfigData& stConfigData)
{
    return _stScrollConfig.GetConfig(usScrollConfigID, stConfigData);
}

int CItemMgr::LoadConfig(const char* szCfgPath)
{
    int iRet = 0;

    if (szCfgPath == NULL)
    {
        CT_ERROR(("Item Cfg Path is null"));
        return RET_SYSTEM_FAIL;
    }

    pb_cfg_item stItemPBConfig;
    std::fstream stFileInput(szCfgPath, std::ios::in | std::ios::binary);
    if (!stItemPBConfig.ParseFromIstream(&stFileInput))
    {
        CT_ERROR(("Failed to Parse ItemCfg! %s", szCfgPath));
        return RET_SYSTEM_FAIL;
    }

    //解析配置文件内容
    //背包配置
    for (int i = 0; i < stItemPBConfig.storage_item_rows_size(); ++i)
    {
        const pb_cfg_item_pb_cfg_storage_item_unit& stItemPBConfigUnit
            = stItemPBConfig.storage_item_rows(i);

        CItemConfigData stConfigData;

        stConfigData._ucItemType = (unsigned char)(stItemPBConfigUnit.item_type() & 0xFF);
        stConfigData._iPosValue = stItemPBConfigUnit.pos_value();
        stConfigData._iUseGrid = (short)(stItemPBConfigUnit.use_gird() & 0x7FFF);

        int iMaxCount = stItemPBConfigUnit.max_count_per_grid();
        if (iMaxCount > 2000000000)
        {
            CT_ERROR(("%d, %d", stConfigData._ucItemType, iMaxCount));
            return ERROR_WRONG_ITEM_TYPE;
        }

        stConfigData._shMaxCountPerGrid = iMaxCount;

        if (stItemPBConfigUnit.is_discardable())
        {
            CT_BIT_SET (stConfigData._ucOperFlag, ITEM_OPER_DISCARD);
        }

        if (stItemPBConfigUnit.is_salable())
        {
            CT_BIT_SET (stConfigData._ucOperFlag, ITEM_OPER_SELL);
        }

        if (stItemPBConfigUnit.is_valuable())
        {
            CT_BIT_SET (stConfigData._ucOperFlag, ITEM_OPER_EXPENSIVE);
        }

        stConfigData._bCanFillInGroundBag = stItemPBConfigUnit.can_add_to_groundbag();

        iRet = _stItemConfig.AddConfig(stConfigData);
        if (iRet)
        {
            CT_ERROR(("%d, %d", iRet, stConfigData._ucItemType));
            return iRet;
        }
    }

    //卷轴配置
    for (int i = 0; i < stItemPBConfig.scroll_config_rows_size(); ++i)
    {
        const pb_cfg_item_pb_cfg_scroll_config_unit& stScrollPBConfigUnit
            = stItemPBConfig.scroll_config_rows(i);

        CScrollConfigData stConfigData;

        stConfigData._usSrollConfigID = (unsigned short)(stScrollPBConfigUnit.scroll_id() & 0x7FFF);
        stConfigData._szDropItemSubType.Set("%s", stScrollPBConfigUnit.dropitem_subtype().c_str());
        stConfigData._usUseLevel = (unsigned short)(stScrollPBConfigUnit.use_level() & 0x7FFF);
        stConfigData._uiContriPrice = (unsigned int)(stScrollPBConfigUnit.contri_cost() & 0x7FFF);
        stConfigData._uiSilverBuyPrice = (unsigned int)stScrollPBConfigUnit.silver_buy_price();
        stConfigData._uiSilverSellPrice = (unsigned int)stScrollPBConfigUnit.silver_sell_price();
        stConfigData._uiGoldBuyPrice = (unsigned int)stScrollPBConfigUnit.gold_buy_price();
        stConfigData._bCanAutoSellInIns = (stScrollPBConfigUnit.auto_sell_in_ins() == 1);

        CScrollMaterial stMaterial;
        stConfigData._astMaterial.Clear();
        stMaterial._usMaterialId = (unsigned short)(stScrollPBConfigUnit.material_1_id() & 0x7FFF);
        stMaterial._usMaterialCnt = (unsigned short)(stScrollPBConfigUnit.material_1_count() & 0x7FFF);
        if (stMaterial._usMaterialId)
        {
            stConfigData._astMaterial.AddOneItem(stMaterial);
        }

        stMaterial._usMaterialId = (unsigned short)(stScrollPBConfigUnit.material_2_id() & 0x7FFF);
        stMaterial._usMaterialCnt = (unsigned short)(stScrollPBConfigUnit.material_2_count() & 0x7FFF);
        if (stMaterial._usMaterialId)
        {
            stConfigData._astMaterial.AddOneItem(stMaterial);
        }

        stMaterial._usMaterialId = (unsigned short)(stScrollPBConfigUnit.material_3_id() & 0x7FFF);
        stMaterial._usMaterialCnt = (unsigned short)(stScrollPBConfigUnit.material_3_count() & 0x7FFF);
        if (stMaterial._usMaterialId)
        {
            stConfigData._astMaterial.AddOneItem(stMaterial);
        }

        stMaterial._usMaterialId = (unsigned short)(stScrollPBConfigUnit.material_4_id() & 0x7FFF);
        stMaterial._usMaterialCnt = (unsigned short)(stScrollPBConfigUnit.material_4_count() & 0x7FFF);
        if (stMaterial._usMaterialId)
        {
            stConfigData._astMaterial.AddOneItem(stMaterial);
        }

        stMaterial._usMaterialId = (unsigned short)(stScrollPBConfigUnit.material_5_id() & 0x7FFF);
        stMaterial._usMaterialCnt = (unsigned short)(stScrollPBConfigUnit.material_5_count() & 0x7FFF);
        if (stMaterial._usMaterialId)
        {
            stConfigData._astMaterial.AddOneItem(stMaterial);
        }

        stConfigData._uiNeedGold = (unsigned int)stScrollPBConfigUnit.need_gold();
        stConfigData._ucNewItemType = (unsigned char)(stScrollPBConfigUnit.type_id()&0xFF);
        if (stConfigData._ucNewItemType == ITEM_TYPE_EQUIP)
        {
            unsigned short usEquipCfgId;
            iRet = CMainCtrl::m_pstMain->m_stEquipMgr.GetScrollEquipCfgId(stConfigData._usSrollConfigID, usEquipCfgId);
            if (iRet)
            {
                CT_WARNING(("Scroll id (%u) can't find equip material id.", stConfigData._usSrollConfigID));
                continue;
            }

            stConfigData._usOldEquipConfigID = usEquipCfgId;
            stConfigData._usNewEquipConfigID = (unsigned short)stScrollPBConfigUnit.construct_item_id() & 0x7FFF;
        }
        else
        {
            stConfigData._usOldEquipConfigID = 0;
            stConfigData._usNewEquipConfigID = 0;
        }

        iRet = _stScrollConfig.AddConfig(stConfigData);
        CHECK_RETURN(iRet);
    }

    //材料配置
    for (int i = 0; i < stItemPBConfig.material_config_rows_size(); ++i)
    {
        const pb_cfg_item_pb_cfg_material_config_unit& stMaterialPBConfigUnit
            = stItemPBConfig.material_config_rows(i);

        unsigned char ucItemType = (unsigned char)(stMaterialPBConfigUnit.type() & 0x7F);
        if (ITEM_TYPE_MATERIAL == ucItemType)
        {
            CMaterialConfigData stConfigData;
            stConfigData._usMaterialConfigID = (unsigned short)(stMaterialPBConfigUnit.material_id() & 0x7FFF);
            stConfigData._usLevel = (unsigned short)(stMaterialPBConfigUnit.level() & 0x7FFF);
            stConfigData._szDropItemSubType.Set("%s", stMaterialPBConfigUnit.dropitem_subtype().c_str());
            stConfigData._uiSilverBuyPrice = (unsigned int)stMaterialPBConfigUnit.silver_buy_price();
            stConfigData._uiSilverSellPrice = (unsigned int)stMaterialPBConfigUnit.silver_sell_price();
            stConfigData._uiContriPrice = (unsigned int)stMaterialPBConfigUnit.contri_cost();
            stConfigData._uiGoldBuyPrice = (unsigned int)stMaterialPBConfigUnit.gold_buy_price();
            stConfigData._iGoldSynPrice = stMaterialPBConfigUnit.gold_syn_price();
            stConfigData._szIconName.Set("%s", stMaterialPBConfigUnit.icon().c_str());
            stConfigData._iDropBeginTime = (int)CTTime(stMaterialPBConfigUnit.drop_begin_time()).GetTime();
            stConfigData._iDropEndTime = (int)CTTime(stMaterialPBConfigUnit.drop_end_time()).GetTime();
            stConfigData._bCanAutoSellInIns = (stMaterialPBConfigUnit.auto_sell_in_ins() == 1);
            iRet = _stMaterialConfig.AddConfig(stConfigData);
            CHECK_RETURN(iRet);
        }

        if (ITEM_TYPE_JEWEL == ucItemType)
        {
            CJewlConfigData stConfigData;
            stConfigData._usJewelId = (unsigned short)(stMaterialPBConfigUnit.material_id() & 0x7FFF);
            stConfigData._szDropItemSubType.Set("%s", stMaterialPBConfigUnit.dropitem_subtype().c_str());
            stConfigData._uiSilverBuyPrice = (unsigned int)stMaterialPBConfigUnit.silver_buy_price();
            stConfigData._uiSilverSellPrice = (unsigned int)stMaterialPBConfigUnit.silver_sell_price();

            iRet = _stJewelConfig.AddConfig(stConfigData);
            CHECK_RETURN(iRet);
        }

        if (ITEM_TYPE_SOLVENT == ucItemType)
        {
            _stMaterialConfig._stSolventConfig._bHasConfig = true;
            _stMaterialConfig._stSolventConfig._usMaterialConfigID = (unsigned short)(stMaterialPBConfigUnit.material_id() & 0x7FFF);
            _stMaterialConfig._stSolventConfig._uiContriPrice = (unsigned int)stMaterialPBConfigUnit.contri_cost();
            _stMaterialConfig._stSolventConfig._uiSilverBuyPrice = (unsigned int)stMaterialPBConfigUnit.silver_buy_price();
            _stMaterialConfig._stSolventConfig._uiSilverSellPrice = (unsigned int)stMaterialPBConfigUnit.silver_sell_price();
            _stMaterialConfig._stSolventConfig._uiGoldBuyPrice = (unsigned int)stMaterialPBConfigUnit.gold_buy_price();
        }

        if (ITEM_TYPE_SEAL == ucItemType)
        {
            if (_stSealCfg._bHasConfig)
            {
                CT_ERROR((""));
                return ERROR_ITEM_INVALID_ITEM_TYPE;
            }

            _stSealCfg._bHasConfig = true;
            _stSealCfg._iBuySilverPrice = stMaterialPBConfigUnit.silver_buy_price();
            _stSealCfg._iSellSilverPrice = stMaterialPBConfigUnit.silver_sell_price();
            _stSealCfg._iBuyContPrice = stMaterialPBConfigUnit.contri_cost();
            _stSealCfg._iBuyGoldPrice = stMaterialPBConfigUnit.gold_buy_price();
        }

        if (ITEM_TYPE_PARTNER == ucItemType)
        {
            CDropPartnerConfigData stConfigData;
            stConfigData._usPartnerID = (unsigned short)(stMaterialPBConfigUnit.material_id() & 0x7FFF);
            stConfigData._uiSilverBuyPrice = (unsigned int)stMaterialPBConfigUnit.silver_buy_price();
            stConfigData._uiSilverSellPrice = (unsigned int)stMaterialPBConfigUnit.silver_sell_price();

            iRet = _stDropPartnerCfg.AddConfig(stConfigData);
            CHECK_RETURN(iRet);
        }
    }

    //魔晶配置
    for (int i = 0; i < stItemPBConfig.crystal_config_rows_size(); ++i)
    {
        const pb_cfg_item_pb_cfg_crystal_config_unit& stCrystalPBConfigUnit
            = stItemPBConfig.crystal_config_rows(i);

        unsigned char ucItemType = (unsigned char)(stCrystalPBConfigUnit.type() & 0x7F);
        if (ITEM_TYPE_CRYSTAL == ucItemType)
        {
            CCrystalConfigData stConfigData;
            stConfigData._usCrystalConfigID = (unsigned short)(stCrystalPBConfigUnit.crystal_id() & 0x7FFF);
            stConfigData._szDropItemSubType.Set("%s", stCrystalPBConfigUnit.dropitem_subtype().c_str());
            stConfigData._usLevel = (unsigned short)(stCrystalPBConfigUnit.level() & 0x7FFF);
            stConfigData._ucAttrType = (unsigned char)(stCrystalPBConfigUnit.attr_type() & 0x7F);
            stConfigData._uiAttrValue = stCrystalPBConfigUnit.attr_value();
            stConfigData._ucPotenAttr = stCrystalPBConfigUnit.pot_attr();
            stConfigData._iPotenValue = stCrystalPBConfigUnit.pot_value();
            stConfigData._szIconName.Set("%s", stCrystalPBConfigUnit.icon().c_str());

            if (strlen(stCrystalPBConfigUnit.ware_pos_limit().c_str()) > 0)
            {
                std::string sWarePosLimit = stCrystalPBConfigUnit.ware_pos_limit();
                const std::string sDel = ",";
                std::vector<std::string> vWarePosLimit;

                CTStringUtils::Split(sWarePosLimit, sDel, vWarePosLimit);
                for (unsigned int i = 0; i < vWarePosLimit.size(); ++i)
                {
                    unsigned char ucWarePos = (unsigned char)atoi(vWarePosLimit[i].c_str());
                    stConfigData.SetCanWarePos(ucWarePos);
                }
            }

            if (strlen(stCrystalPBConfigUnit.crystal_frag_cnt().c_str()) > 0)
            {
                std::string sCrystalFragCnt = stCrystalPBConfigUnit.crystal_frag_cnt();
                const std::string sDel = ",";
                std::vector<std::string> vFragCnt;

                CTStringUtils::Split(sCrystalFragCnt, sDel, vFragCnt);

                if (vFragCnt.size() < 2)
                {
                    CT_ERROR(("crystal config err, frag_cnt_string: %s", stCrystalPBConfigUnit.crystal_frag_cnt().c_str()));
                }

                stConfigData._usDecompFragMin = (unsigned short)atoi(vFragCnt[0].c_str());
                stConfigData._usDecompFragMax = (unsigned short)atoi(vFragCnt[1].c_str());

                if (stConfigData._usDecompFragMin > stConfigData._usDecompFragMax)
                {
                    CT_ERROR(("crystal config err, frag_cnt_string: %s", stCrystalPBConfigUnit.crystal_frag_cnt().c_str()));
                }

                stConfigData._iDecomPrice = stCrystalPBConfigUnit.decompose_price();
            }

            stConfigData._usConstructCrystalID = (unsigned short)(stCrystalPBConfigUnit.construct_crystal_id() & 0x7FFF);
            stConfigData._usRequiredLevel = (unsigned short)(stCrystalPBConfigUnit.construct_level_limit() & 0x7FFF);
            stConfigData._uiGoldBuyPrice = (unsigned int)stCrystalPBConfigUnit.gold_buy_price();
            stConfigData._uiSilverBuyPrice = (unsigned int)stCrystalPBConfigUnit.silver_buy_price();
            stConfigData._uiSilverSellPrice = (unsigned int)stCrystalPBConfigUnit.silver_sell_price();
            stConfigData._uiContriPrice = (unsigned int)stCrystalPBConfigUnit.contri_cost();
            stConfigData._uiInstanceType = (unsigned int)(stCrystalPBConfigUnit.default_drop_instance_type() & 0x7FFFFFFF);
            stConfigData._uiInstanceId = (unsigned int)(stCrystalPBConfigUnit.default_drop_instance_id() & 0x7FFFFFFF);

            iRet = _stCrystalConfig.AddConfig(stConfigData);
            CHECK_RETURN(iRet);
        }

        if (ITEM_TYPE_CRYSTAL_FRAG == ucItemType)
        {
            if (_stCrystalConfig._stCrystalFragCfg._bHasConfig)
            {
                CT_ERROR(("repeated crystal config error"));
                return RET_SYS_ERROR;
            }

            _stCrystalConfig._stCrystalFragCfg._bHasConfig = true;
            _stCrystalConfig._stCrystalFragCfg._usCrystaFraglId = stCrystalPBConfigUnit.crystal_id();
            _stCrystalConfig._stCrystalFragCfg._uiSilverBuyPrice = (unsigned int)stCrystalPBConfigUnit.silver_buy_price();
            _stCrystalConfig._stCrystalFragCfg._uiSilverSellPrice = (unsigned int)stCrystalPBConfigUnit.silver_sell_price();
            _stCrystalConfig._stCrystalFragCfg._iBuyContPrice = stCrystalPBConfigUnit.contri_cost();
        }
    }

    //等级相关物品
    CLevelItemConfigData stConfigData;
    for (int i = 0; i < stItemPBConfig.level_item_rows_size(); ++i)
    {
        const pb_cfg_item_pb_cfg_level_item_unit& stLevelItemPBConfigUnit
            = stItemPBConfig.level_item_rows(i);

        stConfigData.Clear();
        stConfigData._usLevel = stLevelItemPBConfigUnit.level();
        stConfigData._iExpPara = stLevelItemPBConfigUnit.exp_para();
        stConfigData._iSilverPara = stLevelItemPBConfigUnit.silver_para();
        stConfigData._iEnemyAtkCityBonusPara = stLevelItemPBConfigUnit.eac_bonus_para();

        CRateTarget stRateTarget;
        stRateTarget._iRate = 1;
        stRateTarget._iMaxCount = CRateTarget::TARGET_INFINITY;
        const std::string sDel = ",";
        if (strlen(stLevelItemPBConfigUnit.item_set_1().c_str()) > 0)
        {
            std::string sItemSet = stLevelItemPBConfigUnit.item_set_1();
            std::vector<std::string> vItemSet;

            CTStringUtils::Split(sItemSet, sDel, vItemSet);
            for (unsigned int i = 0; i < vItemSet.size(); ++i)
            {
                stRateTarget._iTargetID = atoi(vItemSet[i].c_str());
                stConfigData._stDropSet1.Add(stRateTarget);
            }
        }

        if (strlen(stLevelItemPBConfigUnit.item_set_2().c_str()) > 0)
        {
            std::string sItemSet = stLevelItemPBConfigUnit.item_set_2();
            std::vector<std::string> vItemSet;

            CTStringUtils::Split(sItemSet, sDel, vItemSet);
            for (unsigned int i = 0; i < vItemSet.size(); ++i)
            {
                stRateTarget._iTargetID = atoi(vItemSet[i].c_str());
                stConfigData._stDropSet2.Add(stRateTarget);
            }
        }

        if (strlen(stLevelItemPBConfigUnit.item_set_3().c_str()) > 0)
        {
            std::string sItemSet = stLevelItemPBConfigUnit.item_set_3();
            std::vector<std::string> vItemSet;

            CTStringUtils::Split(sItemSet, sDel, vItemSet);
            for (unsigned int i = 0; i < vItemSet.size(); ++i)
            {
                stRateTarget._iTargetID = atoi(vItemSet[i].c_str());
                stConfigData._stDropSet3.Add(stRateTarget);
            }
        }

        if (strlen(stLevelItemPBConfigUnit.item_set_4().c_str()) > 0)
        {
            std::string sItemSet = stLevelItemPBConfigUnit.item_set_4();
            std::vector<std::string> vItemSet;

            CTStringUtils::Split(sItemSet, sDel, vItemSet);
            for (unsigned int i = 0; i < vItemSet.size(); ++i)
            {
                stRateTarget._iTargetID = atoi(vItemSet[i].c_str());
                stConfigData._stDropSet4.Add(stRateTarget);
            }
        }

        iRet = _stLevelItemConfig.AddConfig(stConfigData);
        CHECK_RETURN(iRet);
    }

    //礼包配置
    for (int i = 0; i < stItemPBConfig.gift_config_rows_size(); ++i)
    {
        const pb_cfg_item_pb_cfg_gift_config_unit & stGiftConfigUnit
            = stItemPBConfig.gift_config_rows(i);

        CGiftConfigUnit stConfigData;

        stConfigData._usGiftId = stGiftConfigUnit.gift_id();
        stConfigData._ucGiftType = stGiftConfigUnit.gift_drop_type();
        stConfigData._usMutexType = stGiftConfigUnit.mutex_type();
        stConfigData._shRequiredLevel = stGiftConfigUnit.required_level();
        stConfigData._iDropRateBase = stGiftConfigUnit.drop_rate_base();
        stConfigData._bDirectOpen = (0 == stGiftConfigUnit.direct_open()  ? false : true);
        stConfigData._iSellSilverPrice = stGiftConfigUnit.silver_sell_price();
        stConfigData._iBuySilverPrice = stGiftConfigUnit.silver_buy_price();
        stConfigData._iBuyGoldPrice = stGiftConfigUnit.gold_buy_price();
        stConfigData._iBuyContPrice = stGiftConfigUnit.contri_cost();
        stConfigData._szIconName.Set("%s", stGiftConfigUnit.icon().c_str());
        stConfigData._iDropBeginTime = (int)CTTime(stGiftConfigUnit.drop_begin_time()).GetTime();
        stConfigData._iDropEndTime = (int)CTTime(stGiftConfigUnit.drop_end_time()).GetTime();
        stConfigData._uiTipsType = stGiftConfigUnit.tips_type();

        CGiftDropItemConfig stItemConfig;
        stItemConfig._stItem._ucItemType = stGiftConfigUnit.item_type_1();
        stItemConfig._stItem._usItemId = stGiftConfigUnit.drop_itemid_1();
        stItemConfig._stItem._uiItemCount = stGiftConfigUnit.item_cnt_1();
        stItemConfig._iDropRate = stGiftConfigUnit.item_rate_1();
        stItemConfig._szItemSubType.Set("%s", stGiftConfigUnit.item_droptype_1().c_str());
        iRet = stConfigData.AddDropItemConfig(stItemConfig);
        CHECK_RETURN(iRet);

        stItemConfig._stItem._ucItemType = stGiftConfigUnit.item_type_2();
        stItemConfig._stItem._usItemId = stGiftConfigUnit.drop_itemid_2();
        stItemConfig._stItem._uiItemCount = stGiftConfigUnit.item_cnt_2();
        stItemConfig._iDropRate = stGiftConfigUnit.item_rate_2();
        stItemConfig._szItemSubType.Set("%s", stGiftConfigUnit.item_droptype_2().c_str());
        iRet = stConfigData.AddDropItemConfig(stItemConfig);
        CHECK_RETURN(iRet);

        stItemConfig._stItem._ucItemType = stGiftConfigUnit.item_type_3();
        stItemConfig._stItem._usItemId = stGiftConfigUnit.drop_itemid_3();
        stItemConfig._stItem._uiItemCount = stGiftConfigUnit.item_cnt_3();
        stItemConfig._iDropRate = stGiftConfigUnit.item_rate_3();
        stItemConfig._szItemSubType.Set("%s", stGiftConfigUnit.item_droptype_3().c_str());
        iRet = stConfigData.AddDropItemConfig(stItemConfig);
        CHECK_RETURN(iRet);

        stItemConfig._stItem._ucItemType = stGiftConfigUnit.item_type_4();
        stItemConfig._stItem._usItemId = stGiftConfigUnit.drop_itemid_4();
        stItemConfig._stItem._uiItemCount = stGiftConfigUnit.item_cnt_4();
        stItemConfig._iDropRate = stGiftConfigUnit.item_rate_4();
        stItemConfig._szItemSubType.Set("%s", stGiftConfigUnit.item_droptype_4().c_str());
        iRet = stConfigData.AddDropItemConfig(stItemConfig);
        CHECK_RETURN(iRet);

        stItemConfig._stItem._ucItemType = stGiftConfigUnit.item_type_5();
        stItemConfig._stItem._usItemId = stGiftConfigUnit.drop_itemid_5();
        stItemConfig._stItem._uiItemCount = stGiftConfigUnit.item_cnt_5();
        stItemConfig._iDropRate = stGiftConfigUnit.item_rate_5();
        stItemConfig._szItemSubType.Set("%s", stGiftConfigUnit.item_droptype_5().c_str());
        iRet = stConfigData.AddDropItemConfig(stItemConfig);
        CHECK_RETURN(iRet);

        stItemConfig._stItem._ucItemType = stGiftConfigUnit.item_type_6();
        stItemConfig._stItem._usItemId = stGiftConfigUnit.drop_itemid_6();
        stItemConfig._stItem._uiItemCount = stGiftConfigUnit.item_cnt_6();
        stItemConfig._iDropRate = stGiftConfigUnit.item_rate_6();
        stItemConfig._szItemSubType.Set("%s", stGiftConfigUnit.item_droptype_6().c_str());
        iRet = stConfigData.AddDropItemConfig(stItemConfig);
        CHECK_RETURN(iRet);

        stItemConfig._stItem._ucItemType = stGiftConfigUnit.item_type_7();
        stItemConfig._stItem._usItemId = stGiftConfigUnit.drop_itemid_7();
        stItemConfig._stItem._uiItemCount = stGiftConfigUnit.item_cnt_7();
        stItemConfig._iDropRate = stGiftConfigUnit.item_rate_7();
        stItemConfig._szItemSubType.Set("%s", stGiftConfigUnit.item_droptype_7().c_str());
        iRet = stConfigData.AddDropItemConfig(stItemConfig);
        CHECK_RETURN(iRet);

        stItemConfig._stItem._ucItemType = stGiftConfigUnit.item_type_8();
        stItemConfig._stItem._usItemId = stGiftConfigUnit.drop_itemid_8();
        stItemConfig._stItem._uiItemCount = stGiftConfigUnit.item_cnt_8();
        stItemConfig._iDropRate = stGiftConfigUnit.item_rate_8();
        stItemConfig._szItemSubType.Set("%s", stGiftConfigUnit.item_droptype_8().c_str());
        iRet = stConfigData.AddDropItemConfig(stItemConfig);
        CHECK_RETURN(iRet);

        stItemConfig._stItem._ucItemType = stGiftConfigUnit.item_type_9();
        stItemConfig._stItem._usItemId = stGiftConfigUnit.drop_itemid_9();
        stItemConfig._stItem._uiItemCount = stGiftConfigUnit.item_cnt_9();
        stItemConfig._iDropRate = stGiftConfigUnit.item_rate_9();
        stItemConfig._szItemSubType.Set("%s", stGiftConfigUnit.item_droptype_9().c_str());
        iRet = stConfigData.AddDropItemConfig(stItemConfig);
        CHECK_RETURN(iRet);

        stItemConfig._stItem._ucItemType = stGiftConfigUnit.item_type_10();
        stItemConfig._stItem._usItemId = stGiftConfigUnit.drop_itemid_10();
        stItemConfig._stItem._uiItemCount = stGiftConfigUnit.item_cnt_10();
        stItemConfig._iDropRate = stGiftConfigUnit.item_rate_10();
        stItemConfig._szItemSubType.Set("%s", stGiftConfigUnit.item_droptype_10().c_str());
        iRet = stConfigData.AddDropItemConfig(stItemConfig);
        CHECK_RETURN(iRet);

        for (int i = 0; i < stConfigData._astToDropItems.Size(); i++)
        {
            CGiftDropItemConfig & stCfg = stConfigData._astToDropItems[i];
            if (GIFT_TYPE_FIXED == stConfigData._ucGiftType && IsLevelItem(stCfg._stItem._ucItemType, stCfg._stItem._usItemId))
            {
                CT_ERROR(("%d", stConfigData._usGiftId));
                return ERROR_GIFT_CONFIG_INVALID;
            }

            if (0 == stCfg._stItem._uiItemCount)
            {
                CT_ERROR(("%d, %d, %d", stConfigData._usGiftId, stCfg._stItem._ucItemType, i));
                return ERROR_GIFT_CONFIG_INVALID;
            }

            if (stCfg._szItemSubType.GetDataLen() > 0)
            {
                CDropItemData stData;
                iRet = GetDropItemDataByKey(stCfg._stItem._ucItemType, stCfg._szItemSubType(), stCfg._stItem._uiItemCount, stData);
                if (iRet)
                {
                    CT_ERROR(("%d, %d, %s", stConfigData._usGiftId, i, stCfg._szItemSubType()));
                    return ERROR_GIFT_CONFIG_INVALID;
                }
            }
        }

        iRet = _stGiftConfig.AddConfig(stConfigData);
        CHECK_RETURN(iRet);
    }

    //宠物果实配置
    for (int i = 0; i < stItemPBConfig.pet_fruit_rows_size(); ++i)
    {
        const pb_cfg_item_pb_cfg_pet_fruit_unit & stFruitUnitConfig
            = stItemPBConfig.pet_fruit_rows(i);

        CFruitUnitConfig stConfigData;
        stConfigData._bHasConfig = false;
        stConfigData._ucItemType = stFruitUnitConfig.type();
        stConfigData._usFruitId = stFruitUnitConfig.material_id();
        stConfigData._ucLevel = stFruitUnitConfig.fruit_level();
        stConfigData._ucStar = stFruitUnitConfig.fruit_star();
        stConfigData._iRecoverGoldCost = stFruitUnitConfig.recover_cost();
        stConfigData._iSellSilver = stFruitUnitConfig.silver_sell_price();
        iRet = _stFruitCfg.AddConfig(stConfigData);
        CHECK_RETURN(iRet);
    }

    return 0;
}

int CItemMgr::GetConfig(unsigned char ucItemType, unsigned int uiItemID, CItemConfigData& stConfigData)
{
    int iRet = 0;
    int iPosValue = 0;

    switch(ucItemType)
    {
    case ITEM_TYPE_EQUIP:
        {
            CEquipConfigData stEquipConfigData;
            iRet = CMainCtrl::m_pstMain->m_stEquipMgr.GetConfigData(uiItemID, stEquipConfigData);
            CHECK_RETURN(iRet);

            iPosValue = stEquipConfigData._ucEquipLevel * EQUIP_POS_MAX + stEquipConfigData._ucEquipPos;
        }
        break;
    case ITEM_TYPE_SCROLL:
        {
            unsigned short usScrollConfigID = (unsigned short)(uiItemID & 0xFFFF);
            CScrollConfigData stScrollConfigData;
            iRet = GetScrollConfig(usScrollConfigID, stScrollConfigData);
            CHECK_RETURN(iRet);

            iPosValue = stScrollConfigData._usUseLevel * MAX_SCROLL_CONFIG_COUNT + stScrollConfigData._usSrollConfigID;
        }
        break;
    case ITEM_TYPE_MATERIAL:
        {
            unsigned short usMaterialConfigID = (unsigned short)(uiItemID & 0xFFFF);
            CMaterialConfigData stMaterialConfigData;
            iRet = GetMaterialConfig(usMaterialConfigID, stMaterialConfigData);
            CHECK_RETURN(iRet);
            iPosValue = stMaterialConfigData._usLevel * MAX_MATERIAL_CONFIG_COUNT + stMaterialConfigData._usMaterialConfigID;
        }
        break;
    case ITEM_TYPE_CRYSTAL:
        {
            unsigned short usCrystalConfigID = (unsigned short)(uiItemID & 0x7FFF);
            CCrystalConfigData stCrystalConfigData;
            iRet = GetCrystalConfig(usCrystalConfigID, stCrystalConfigData);
            CHECK_RETURN(iRet);
            iPosValue = (EXT_ATTR_MAX_COUNT - stCrystalConfigData._ucAttrType) + (stCrystalConfigData._usLevel * EXT_ATTR_MAX_COUNT) ;
        }
        break;
    }

    //获取背包中排序的权值
    iRet = _stItemConfig.GetConfig(ucItemType, stConfigData);
    if (iRet != 0)
    {
        return iRet;
    }

    stConfigData._iPosValue += iPosValue;
    return 0;
}

int CItemMgr::GetCrystalAttr (unsigned short usCrystalConfigID, CAttrPair & stAttr)
{
    return _stCrystalConfig.GetCrystalAttrAdd(usCrystalConfigID, stAttr);
}

int CItemMgr::GetPotenAttrAdd (unsigned short usCrystalConfigID, CAttrPair & stAttr)
{
    CCrystalConfigData stConfigData;

    int iRet = _stCrystalConfig.GetConfig (usCrystalConfigID, stConfigData);
    CHECK_RETURN(iRet);

    stAttr._ucType = stConfigData._ucPotenAttr;
    stAttr._iValue = stConfigData._iPotenValue;
    return 0;
}

int CItemMgr::GetDropItemDataByKey (unsigned char ucItemType, const char* sKey, int iCount, CDropItemData& stDropItemData)
{
    if (NULL == sKey)
    {
        return -1;
    }

    CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY szKey;
    szKey.Set("%s", sKey);

    int iRet = RET_OK;
    unsigned short usItemConfigID = 0;
    switch (ucItemType)
    {
    case ITEM_TYPE_EQUIP:
        {
            CEquipConfigData stEquipConfigData;
            iRet = CMainCtrl::m_pstMain->m_stEquipMgr.GetConfigData(szKey, stEquipConfigData);
            if (RET_OK == iRet)
            {
                usItemConfigID = stEquipConfigData._usEquipConfigID;
            }
        }
        break;
    case ITEM_TYPE_SCROLL:
        {
            CScrollConfigData stScrollConfigData;
            iRet = _stScrollConfig.GetConfig(szKey, stScrollConfigData);
            if (iRet == RET_OK)
            {
                usItemConfigID = stScrollConfigData._usSrollConfigID;
            }
        }
        break;
    case ITEM_TYPE_MATERIAL:
        {
            CMaterialConfigData stMaterialConfigData;
            iRet = _stMaterialConfig.GetConfig(szKey, stMaterialConfigData);
            if (iRet == RET_OK)
            {
                usItemConfigID = stMaterialConfigData._usMaterialConfigID;
            }
        }
        break;
    case ITEM_TYPE_CRYSTAL:
        {
            CCrystalConfigData stCrystalConfigData;
            iRet = _stCrystalConfig.GetConfig(szKey, stCrystalConfigData);
            if (iRet == RET_OK)
            {
                usItemConfigID = stCrystalConfigData._usCrystalConfigID;
            }
        }
        break;
    default:
        return -2;
    }

    if (iRet || 0 == usItemConfigID)
    {
        CT_ERROR(("%d, %d, %s", iRet, usItemConfigID, sKey));
        return -3;
    }

    stDropItemData._iItemCount = iCount;
    stDropItemData._usItemConfigID = usItemConfigID;
    return 0;
}

int CItemMgr::GetDropItemData(unsigned int uiGID, unsigned char ucItemType, unsigned int uiItemID, const char* sKey,
                              unsigned int uiDropRate, int iDropCount, CDropItemData& stDropItemData)
{
    stDropItemData.Clear();

    if (ucItemType < ITEM_TYPE_MIN || ucItemType > ITEM_TYPE_MAX)
    {
        return ERROR_STORAGE_NO_ITEM_CONFIG;
    }

    if (iDropCount <= 0)
    {
        return 0;
    }

    stDropItemData._ucItemType = ucItemType;
    stDropItemData._usItemConfigID = (unsigned short)uiItemID;
    stDropItemData._iItemCount = 0;

    if (uiDropRate < COMM_MOD  && CT_RANDOM_NUM->GetRand(COMM_MOD) >= (int)uiDropRate) //未ROLL中
    {
        return 0;
    }

    int iRet = RET_OK;
    if ((0 == uiItemID) && (NULL != sKey) && (strlen(sKey) > 0))   //根据串掉落
    {
        iRet = GetDropItemDataByKey(ucItemType, sKey, iDropCount, stDropItemData);
        CHECK_RETURN(iRet);
    }
    else
    {
        if (IsLevelItem(stDropItemData._ucItemType, stDropItemData._usItemConfigID))   //根据等级掉落的物品
        {
            iRet = GetLevelItem(uiGID, stDropItemData._ucItemType, stDropItemData._usItemConfigID, iDropCount, stDropItemData);
            if (iRet)
            {
                CT_ERROR(("%d, %d, %d", stDropItemData._ucItemType, stDropItemData._usItemConfigID, iRet));
                return iRet;
            }
        }
        else
        {
            stDropItemData._usItemConfigID = uiItemID;
            stDropItemData._iItemCount = iDropCount;

            switch(ucItemType)
            {
            case ITEM_TYPE_TREASURE:
                {
                    int iIndex = CMainCtrl::m_pstMain->m_stPlayerDetailInfoHash.HashFind(uiGID);
                    if (iIndex < 0)
                    {
                        CT_ERROR(("%u", uiGID));
                        return RET_SYS_ERROR;
                    }
                    CPlayerDetailInfo& stPlayerDetailInfo = CMainCtrl::m_pstMain->m_stPlayerDetailInfoHash[iIndex];

                    if (CMainCtrl::m_pstMain->m_stTreasureMgr.IsRoleGetTreasure(stDropItemData._usItemConfigID, stPlayerDetailInfo))
                    {
                        stDropItemData._iItemCount = 0;  //已经获得秘宝返回0
                    }
                }
                break;
            }
        }
    }

    if (iRet)
    {
        CT_ERROR(("%d, %d, %d, %s", uiGID, ucItemType, iRet, sKey));
        return iRet;
    }

    //检查掉落时间
    int iDropBeginTime = 0;
    int iDropEndTime = 0;
    switch (stDropItemData._ucItemType)
    {
    case ITEM_TYPE_MATERIAL:
        {
            CMaterialConfigData stConfigData;
            iRet = _stMaterialConfig.GetConfig(stDropItemData._usItemConfigID, stConfigData);
            CHECK_RETURN(iRet);

            iDropBeginTime = stConfigData._iDropBeginTime;
            iDropEndTime = stConfigData._iDropEndTime;
        }
        break;
    case ITEM_TYPE_GIFT:
        {
            CGiftConfigUnit stConfigData;
            iRet = _stGiftConfig.GetGiftConfig(stDropItemData._usItemConfigID, stConfigData);
            CHECK_RETURN(iRet);

            iDropBeginTime = stConfigData._iDropBeginTime;
            iDropEndTime = stConfigData._iDropEndTime;
        }
        break;
    default:
        break;
    }

    if ((iDropBeginTime != 0 && iDropBeginTime > CMainCtrl::m_pstMain->m_iNow)
        || (iDropEndTime != 0 && iDropEndTime < CMainCtrl::m_pstMain->m_iNow))
    {
        stDropItemData._iItemCount = 0;
        return 0;
    }

    return 0;
}

int CItemMgr::DropItem(CTAsyncCmd* pstParent, unsigned int uiGID, CDropItem& stDropItem, bool bCheckGridEnough)
{
    if (bCheckGridEnough && !stDropItem.HasEnoughSpace(uiGID))
    {
        return ERROR_BAG_WILL_FULL;
    }

    CCmdDropItem* pstCmd = (CCmdDropItem*)CT_CMDFACTORY->CreateCmd(ID_MAIN_INNER_DROP_ITEM);
    if (pstCmd == NULL)
    {
        CT_ERROR(("create CCmdDropItem error"));
        return RET_SYSTEM_FAIL;
    }

    pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
    pstCmd->stDropItem = stDropItem;
    pstCmd->uiGID = uiGID;

    if (pstParent != NULL)
    {
        pstParent->RegisterChild(pstCmd);
    }

    return pstCmd->Do(NULL);
}

int CItemMgr::DropItemEx(CTAsyncCmd* pstParent, unsigned int uiGID, CDropItem& stDropItem)
{
    CCmdDropToExSpace* pstCmd = (CCmdDropToExSpace*)CT_CMDFACTORY->CreateCmd(ID_MAIN_INNER_DROP_TO_EXSPACE);
    if (pstCmd == NULL)
    {
        CT_ERROR(("create CCmdDropToExSpace error"));
        return RET_SYSTEM_FAIL;
    }

    pstCmd->pstMainCtrl = CMainCtrl::m_pstMain;
    pstCmd->stDropItem = stDropItem;
    pstCmd->uiGID = uiGID;

    if (pstParent != NULL)
    {
        pstParent->RegisterChild(pstCmd);
    }

    return pstCmd->Do(NULL);
}

int CItemMgr::GetDropLeftItem(CTLib::CTAsyncCmd* pstParent, CDropItem& stLeftItem)
{
    if (pstParent == NULL || pstParent->GetChild() == NULL || pstParent->GetChild()->GetCmdID() != ID_MAIN_INNER_DROP_ITEM)
    {
        return 0;
    }

    stLeftItem.Clear();
    CCmdDropItem* pstCmd = (CCmdDropItem*)(pstParent->GetChild());
    stLeftItem = pstCmd->stLeftItem;
    return 0;
}

int CItemMgr::GetCombinedCrystal (unsigned short usOriginCrystal, CCrystalConfigData& stConfigData)
{
    CCrystalConfigData stOriginCrystal;
    int iRet = GetCrystalConfig(usOriginCrystal, stOriginCrystal);
    CHECK_RETURN(iRet);

    iRet = GetCrystalConfig(stOriginCrystal._usConstructCrystalID, stConfigData);
    CHECK_RETURN(iRet);

    return 0;
}

int CItemMgr::GetItemPrice(unsigned char ucItemType, unsigned short usConfigID, unsigned short usCount, EN_ITEM_PRICE_TYPE enPriceType, int &iPrices)
{
    int iRet = 0;
    int iBuyGoldPrices = 0X7FFFFFFF;     //金币购买价格
    int iBuySilverPrices = 0X7FFFFFFF;   //银币购买价格
    int iBuyContriPrices = 0X7FFFFFFF;   //贡献度购买价格
    int iSellPrices = 0;              //银币出售价格
    switch(ucItemType)
    {
    case ITEM_TYPE_MATERIAL:
        {
            CMaterialConfigData stMaterialConfigData;
            iRet = CMainCtrl::m_pstMain->m_stItemMgr.GetMaterialConfig(usConfigID, stMaterialConfigData);
            CHECK_RETURN(iRet);
            iBuyGoldPrices = stMaterialConfigData._uiGoldBuyPrice;
            iBuySilverPrices = stMaterialConfigData._uiSilverBuyPrice;
            iBuyContriPrices = stMaterialConfigData._uiContriPrice;
            iSellPrices = stMaterialConfigData._uiSilverSellPrice;
        }
        break;
    case ITEM_TYPE_SCROLL:
        {
            CScrollConfigData stScrollConfigData;
            iRet = CMainCtrl::m_pstMain->m_stItemMgr.GetScrollConfig(usConfigID, stScrollConfigData);
            CHECK_RETURN(iRet);
            iBuyGoldPrices = stScrollConfigData._uiGoldBuyPrice;
            iBuySilverPrices = stScrollConfigData._uiSilverBuyPrice;
            iBuyContriPrices = stScrollConfigData._uiContriPrice;
            iSellPrices = stScrollConfigData._uiSilverSellPrice;
        }
        break;
    case ITEM_TYPE_CRYSTAL:
        {
            CCrystalConfigData stCrystalConfigData;
            iRet = CMainCtrl::m_pstMain->m_stItemMgr.GetCrystalConfig(usConfigID, stCrystalConfigData);
            CHECK_RETURN(iRet);

            iBuyGoldPrices = stCrystalConfigData._uiGoldBuyPrice;
            iBuySilverPrices = stCrystalConfigData._uiSilverBuyPrice;
            iSellPrices = stCrystalConfigData._uiSilverSellPrice;
            iBuyContriPrices = stCrystalConfigData._uiContriPrice;
        }
        break;
    case ITEM_TYPE_CRYSTAL_FRAG:
        {
            iBuySilverPrices = _stCrystalConfig._stCrystalFragCfg._uiSilverBuyPrice;
            iSellPrices = _stCrystalConfig._stCrystalFragCfg._uiSilverSellPrice;
            iBuyContriPrices = _stCrystalConfig._stCrystalFragCfg._iBuyContPrice;
        }
        break;

    case ITEM_TYPE_SOLVENT:
        {
            iBuyContriPrices = _stMaterialConfig._stSolventConfig._uiContriPrice;
            iBuySilverPrices = _stMaterialConfig._stSolventConfig._uiSilverBuyPrice;
            iSellPrices = _stMaterialConfig._stSolventConfig._uiSilverSellPrice;
        }
        break;

    case ITEM_TYPE_JEWEL:
        {
            CJewlConfigData stJewlCfg;
            iRet = CMainCtrl::m_pstMain->m_stItemMgr.GetJewelConfig(usConfigID, stJewlCfg);
            CHECK_RETURN(iRet);
            iBuySilverPrices = stJewlCfg._uiSilverBuyPrice;
            iSellPrices = stJewlCfg._uiSilverSellPrice;
        }
        break;

    case ITEM_TYPE_EQUIP:
        {
            CEquipConfigData stEquipConfig;
            iRet = CMainCtrl::m_pstMain->m_stEquipMgr.GetConfigData(usConfigID, stEquipConfig);
            CHECK_RETURN(iRet);

            iBuySilverPrices = stEquipConfig._uiSilverBuyPrice;
            iSellPrices = stEquipConfig._uiSilverSellPrice;
        }
        break;

    case ITEM_TYPE_FRUIT:
        {
            CFruitUnitConfig stFruitConfig;
            iRet = CMainCtrl::m_pstMain->m_stItemMgr.GetFruitConfig(usConfigID, stFruitConfig);
            CHECK_RETURN(iRet);

            iBuySilverPrices = 0X7FFFFFFF;
            iSellPrices = stFruitConfig._iSellSilver;
        }
        break;

    case ITEM_TYPE_SEAL:
        {
            iBuySilverPrices = _stSealCfg._iBuySilverPrice;
            iBuyContriPrices = _stSealCfg._iBuyContPrice;
            iSellPrices = _stSealCfg._iSellSilverPrice;
        }
        break;

    case ITEM_TYPE_GIFT:
        {
            CGiftConfigUnit stGiftConfig;
            iRet = GetGiftConfig (usConfigID, stGiftConfig);
            CHECK_RETURN(iRet);

            iBuySilverPrices = stGiftConfig._iBuySilverPrice;
            iBuyGoldPrices = stGiftConfig._iBuyGoldPrice;
            iBuyContriPrices = stGiftConfig._iBuyContPrice;
            iSellPrices = stGiftConfig._iSellSilverPrice;
        }
        break;

    case ITEM_TYPE_PARTNER:
        {
            CDropPartnerConfigData stConfigData;
            iRet = _stDropPartnerCfg.GetConfig(usConfigID, stConfigData);
            CHECK_RETURN(iRet);

            iBuySilverPrices = stConfigData._uiSilverBuyPrice;
            iSellPrices = stConfigData._uiSilverSellPrice;
        }
        break;

    case ITEM_TYPE_MOUNT:
        {
            if (false == CMainCtrl::m_pstMain->m_stMountMgr.HasThisMount(usConfigID))
            {
                return ERROR_MOUNT_CONFIG_NONE;
            }

            iBuySilverPrices = 0X7FFFFFFF;
            iBuyGoldPrices = 0X7FFFFFFF;
        }
        break;

    case ITEM_TYPE_FASHION:
        {
            if (false == CMainCtrl::m_pstMain->m_stFashionMgr.HasThisFashion(usConfigID))
            {
                return ERROR_FASION_CONFIG;
            }

            iBuySilverPrices = 0X7FFFFFFF;
            iBuyGoldPrices = 0X7FFFFFFF;
        }
        break;

    default:
        CT_ERROR(("%d", ucItemType));
        return ERROR_GET_ITEM_PRICE;
    }

    iBuyGoldPrices = iBuyGoldPrices * usCount;
    iBuySilverPrices = iBuySilverPrices * usCount;
    iBuyContriPrices = iBuyContriPrices * usCount;
    iSellPrices = iSellPrices * usCount;
    switch (enPriceType)
    {
    case ITEM_BUY_PRICE_GOLD:
        {
            iPrices = iBuyGoldPrices;
        }
        break;
    case ITEM_BUY_PRICE_SILVER:
        {
            iPrices = iBuySilverPrices;
        }
        break;
    case ITEM_BUY_PRICE_CONTRI:
        {
            iPrices = iBuyContriPrices;
        }
        break;
    case ITEM_SELL_PRICE:
        {
            iPrices = iSellPrices;
        }
        break;
    default:
        return ERROR_ITEM_PRICE_TYPE;
    }

    return 0;
}

int CItemMgr::GetItemDropType (unsigned char ucItemType, unsigned int uiItemId,
                               CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey)
{
#if 0
    int iRet;
    unsigned short usItemId = (unsigned short)uiItemId;
    switch (ucItemType)
    {
    case ITEM_TYPE_CRYSTAL:
        {
            CCrystalConfigData stConfigData;
            iRet = _stCrystalConfig.GetConfig(usItemId, stConfigData);
            CHECK_RETURN(iRet);

            szKey = stConfigData._szDropItemSubType;
        }
        break;

    case ITEM_TYPE_MATERIAL:
        {
            CMaterialConfigData stConfigData;
            iRet = _stMaterialConfig.GetConfig(usItemId, stConfigData);
            CHECK_RETURN(iRet);

            szKey = stConfigData._szDropItemSubType;
        }
        break;

    case ITEM_TYPE_SCROLL:
        {
            CScrollConfigData stConfigData;
            iRet = _stScrollConfig.GetConfig(usItemId, stConfigData);
            CHECK_RETURN(iRet);

            szKey = stConfigData._szDropItemSubType;
        }
        break;
    case ITEM_TYPE_EQUIP:
        {
            CEquipConfigData stConfigData;
            iRet = CMainCtrl::m_pstMain->m_stEquipMgr.GetConfigData(uiItemId, stConfigData);
            CHECK_RETURN(iRet);

            szKey = stConfigData._szDropItemSubType;
        }
    default:
        return ERROR_ITEM_INVALID_ITEM_TYPE;
    }
#endif
    return 0;
}

int CItemMgr::GetItemDropInstance(unsigned char ucItemType, unsigned short usItemId, CDropInstanceConfig & stDropInstances)
{
    int iRet;
    if (ITEM_TYPE_MATERIAL == ucItemType)
    {
        CMaterialConfigData stConfigData;
        iRet = _stMaterialConfig.GetConfig(usItemId, stConfigData);
        CHECK_RETURN(iRet);

        stDropInstances = stConfigData._stDropInstace;
        return 0;
    }

    if (ITEM_TYPE_SCROLL == ucItemType)
    {
        CScrollConfigData stConfigData;
        iRet = _stScrollConfig.GetConfig(usItemId, stConfigData);
        CHECK_RETURN(iRet);

        stDropInstances = stConfigData._stDropInstace;
        return 0;
    }

    return ERROR_INSTANCE_ADD_CONFIG;
}

bool CItemMgr::FindItemDropInstance(CDropInstanceConfig & stDropInstances, CInstanceId & stCurIns, CInstanceId & stDropId)
{
    // 可以打开的还是未打开的？ (打开>未打开)
    CInstanceId stUnOpenId;   //未打开的, ID最小的,
    stUnOpenId._usInstanceID = 0XFFFF;
    for (int i = 0; i < stDropInstances._astInstanceId.Size(); i++)
    {
        CInstanceId & stConfigId = stDropInstances._astInstanceId[i];
        if (stConfigId._ucInstanceType == stCurIns._ucInstanceType)
        {
            if ((stConfigId._usInstanceID > stCurIns._usInstanceID)
                && stConfigId._usInstanceID <= stUnOpenId._usInstanceID)
            {
                stUnOpenId = stConfigId;
            }

            if (stConfigId._usInstanceID <= stCurIns._usInstanceID
                && stConfigId._usInstanceID > stDropId._usInstanceID)
            {
                stDropId = stConfigId;
            }
        }
    }

    if (stDropId._usInstanceID > 0)
    {
        return true;
    }

    if (stUnOpenId._usInstanceID != 0XFFFF)
    {
        stDropId = stUnOpenId;
        return true;
    }

    return false;
}

int CItemMgr::CheckCanOper (unsigned char ucItemType, unsigned int uiItemId, unsigned char ucOperType, bool & bRetFlag)
{
    CItemConfigData stItemConfigData;
    int iRet = GetConfig(ucItemType, uiItemId, stItemConfigData);
    CHECK_RETURN (iRet);

    bRetFlag = CT_BIT_TEST (stItemConfigData._ucOperFlag, ucOperType);
    return 0;
}

bool CItemMgr::CheckBagFull(CUserStorage& stUserStorage, CDropItem& stDropItem)
{
    int iRet = 0;

    int iNeedBagGrid = 0;
    for (int i = 0; i < stDropItem._astDropItem.Size(); ++i)
    {
        CDropItemData& stDropItemData = stDropItem._astDropItem[i];
        CItemConfigData stItemConfigData;
        iRet = GetConfig(stDropItemData._ucItemType, stDropItemData._usItemConfigID, stItemConfigData);
        CHECK_RETURN(iRet);

        iNeedBagGrid += stDropItem.GetSpaceSize(stDropItemData);
    }

    if (iNeedBagGrid > 0 && iNeedBagGrid > stUserStorage.GetBagEmptyGridCount())
    {
        return true;
    }

    return false;
}

int CItemMgr::AddInstanceDropConfig(CInstanceConfigData & stInstanceConfig)
{
    CInstanceId stInsId;
    stInsId._ucInstanceType = stInstanceConfig._ucInstanceType;
    stInsId._usInstanceID = stInstanceConfig._usInstanceID;
    for (int i = 0; i < stInstanceConfig._astBossDropItemGroup.Size(); i++)
    {
        AddInsGroupConfig(stInstanceConfig._astBossDropItemGroup[i], stInsId);
    }

    for (int i = 0; i < stInstanceConfig._astNormalDropItemGroup.Size(); i++)
    {
        AddInsGroupConfig(stInstanceConfig._astNormalDropItemGroup[i], stInsId);
    }

    for (int i = 0; i < stInstanceConfig._astInstanceDropItemGroup.Size(); i++)
    {
        AddInsGroupConfig(stInstanceConfig._astInstanceDropItemGroup[i], stInsId);
    }

    return 0;
}

void CItemMgr::AddInsGroupConfig(CInstanceDropItemConfigData & stInsDropConfig, CInstanceId & stInstId)
{
    if (ITEM_TYPE_MATERIAL != stInsDropConfig._ucDropItemType
        && ITEM_TYPE_SCROLL != stInsDropConfig._ucDropItemType)
    {
        return ;
    }

    if (0 == stInsDropConfig._iDropCount || 0 == stInsDropConfig._uiDropRate)
    {
        return ;
    }

    if (0 != stInsDropConfig._usDropItemConfigID)
    {
        AddInsItemConfig(stInsDropConfig._ucDropItemType, stInsDropConfig._usDropItemConfigID, stInstId);
    }
    else
    {
        if (stInsDropConfig._szDropItemSubType.GetDataLen() > 0)
        {
            AddInsSubtypeConfig(stInsDropConfig._ucDropItemType, stInsDropConfig._szDropItemSubType, stInstId);
        }
    }
}

void CItemMgr::AddInsSubtypeConfig (unsigned char ucItemType, CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey, CInstanceId & stInstId)
{
    CDropItemSubTypeConfig::T_DROP_ITEM_SUBTYPE_CONFIG_DATA  astData;
    if (ITEM_TYPE_MATERIAL == ucItemType)
    {
        _stMaterialConfig._stDropItemSubTypeConfig.GetAllSubTypeItem (szKey, astData);
    }
    else
    {
        _stScrollConfig._stDropItemSubTypeConfig.GetAllSubTypeItem(szKey, astData);
    }

    for (int i = 0; i < astData.Size(); i++)
    {
        unsigned short usItemId = (unsigned short)astData[i]._iItemID;
        AddInsItemConfig(ucItemType, usItemId, stInstId);
    }
}

void CItemMgr::AddInsItemConfig (unsigned char ucItemType, unsigned short usItemId, CInstanceId & stInstId)
{
    if (ITEM_TYPE_MATERIAL == ucItemType)
    {
        if (_stMaterialConfig.IsMaterialValid(usItemId))
        {
            CMaterialConfigData & stConfigData = _stMaterialConfig._astMaterialConfigDatas[usItemId];
            stConfigData._stDropInstace.AddInstanceId(stInstId);
        }
    }
    else
    {
        if (_stScrollConfig.IsScrollValid(usItemId))
        {
            CScrollConfigData & stConfigData = _stScrollConfig._astScrollConfigDatas[usItemId];
            stConfigData._stDropInstace.AddInstanceId(stInstId);
        }
    }

    CT_TRACE(("%d, %d, %d, %d.", ucItemType, usItemId, stInstId._ucInstanceType, stInstId._usInstanceID));
}

int CItemMgr::GetItemsInGift(unsigned int uiGid, unsigned int uiGfitID, CDropItem & stGiftItem)
{
    if (!_stGiftConfig.IsGiftValid(uiGfitID))
    {
        return ERROR_GIFT_CONFIG_INVALID;
    }

    CGiftConfigUnit & stConfigData = _stGiftConfig._astGifts[uiGfitID];

    int iRet = 0; 
    CDropItemData stToAddItem;
    switch (stConfigData._ucGiftType)
    {
    case GIFT_TYPE_SEPERATE:
        {
            for (int i = 0; i < stConfigData._astToDropItems.Size(); i++)
            {
                CGiftDropItemConfig & stItemConfig = stConfigData._astToDropItems[i];
                if (CT_RANDOM_NUM->GetRand(stConfigData._iDropRateBase) < stItemConfig._iDropRate)
                {
                    iRet = GetGiftItem (uiGid, stItemConfig, stToAddItem);
                    if (iRet || 0 == stToAddItem._iItemCount)
                    {
                        CT_ERROR(("%d, %d, %d", uiGfitID, i, iRet));
                        continue;
                    }

                    stGiftItem.Drop(stToAddItem);
                }
            }
        }
        break;

    case GIFT_TYPE_TOTAL:
        {
            CRandomSet<MAX_GFIT_CONFIG_ITEMS> stRandom;
            for (int i = 0; i < stConfigData._astToDropItems.Size(); i++)
            {
                CRateTarget stDrop;
                stDrop._iTargetID = i;
                stDrop._iMaxCount = CRateTarget::TARGET_INFINITY;
                stDrop._iRate = stConfigData._astToDropItems[i]._iDropRate;
                stRandom.Add(stDrop);
            }

            if (stRandom.GetMax() != stConfigData._iDropRateBase)
            {
                CT_ERROR(("%d, %d, %d", uiGfitID, stRandom.GetMax(), stConfigData._iDropRateBase));
            }

            CRateTarget stTarget;
            iRet = stRandom.DropOne(stTarget);
            if (iRet || (stTarget._iTargetID > MAX_GFIT_CONFIG_ITEMS || stTarget._iTargetID < 0))
            {
                CT_ERROR(("%d, %d, %d", uiGfitID, iRet, stTarget._iTargetID));
                return ERROR_GIFT_DROP_WRONG;
            }

            iRet = GetGiftItem (uiGid, stConfigData._astToDropItems[stTarget._iTargetID], stToAddItem);
            if (iRet || 0 == stToAddItem._iItemCount)
            {
                CT_ERROR(("%d, %d, %d", uiGfitID, stTarget._iTargetID, iRet));
                return ERROR_GIFT_DROP_WRONG;
            }

            if (CLAN_DAIL_GIFT_ID == uiGfitID || CFriendMgr::QQ_FRIEND_AWARD_GIFT_ID == uiGfitID)
            {
                stToAddItem._iData1 = stTarget._iTargetID; // 保存家族转盘QQ好友邀请转盘得到的物品下标(下标从0开始)
            }

            stGiftItem.Drop(stToAddItem);
        }
        break;

    case GIFT_TYPE_FIXED:
        {
            for (int i = 0; i < stConfigData._astToDropItems.Size(); i++)
            {
                CGiftDropItemConfig & stItemConfig = stConfigData._astToDropItems[i];
                stItemConfig._iDropRate = COMM_MOD;

                iRet = GetGiftItem (uiGid, stItemConfig, stToAddItem);
                if (iRet || 0 == stToAddItem._iItemCount)
                {
                    CT_ERROR(("%d, %d, %d", uiGfitID, i, iRet));
                    continue;
                }

                stGiftItem.Drop(stToAddItem);
            }
        }
        break;

    default:
        return ERROR_GIFT_CONFIG_INVALID;
    }

    return RET_OK;
}

int CItemMgr::GetGiftItem(unsigned int uiGfitID, unsigned int uiGid, CDropItem & stGiftItem, bool bPreView)
{
    stGiftItem.Clear();
    int iIndex = CMainCtrl::m_pstMain->m_stPlayerSnsCommInfoHash.HashFind(uiGid);
    if (iIndex < 0)
    {
        CT_ERROR(("%u", uiGid));
        return RET_NO_USER;
    }
    short shLevel = CMainCtrl::m_pstMain->m_stPlayerSnsCommInfoHash[iIndex]._usLevel;

    if (!_stGiftConfig.IsGiftValid(uiGfitID))
    {
        return ERROR_GIFT_CONFIG_INVALID;
    }

    CGiftConfigUnit & stConfigData = _stGiftConfig._astGifts[uiGfitID];

    if (true == bPreView && (GIFT_TYPE_FIXED != stConfigData._ucGiftType))  //只有第三种类型,支持预览
    {
        return ERROR_GIFT_PREVIEW;
    }

    if (false == bPreView && shLevel < stConfigData._shRequiredLevel) //打开时,判断等级
    {
        return ERROR_GIFT_OPEN_LEVEL;
    }

    int iRet = RET_OK;
    iRet = GetItemsInGift(uiGid, uiGfitID, stGiftItem);
    CHECK_RETURN(iRet);

    if (509 == uiGfitID || 511 == uiGfitID || 513 == uiGfitID || //感恩节抽奖礼包的特殊处理
        558 == uiGfitID || 559 == uiGfitID || 560 == uiGfitID ||
        561 == uiGfitID || 562 == uiGfitID || 563 == uiGfitID ||
        576 == uiGfitID ||
        564 == uiGfitID || 565 == uiGfitID || 566 == uiGfitID ||
        567 == uiGfitID || 568 == uiGfitID || 569 == uiGfitID ||
        577 == uiGfitID) //幸运转盘抽奖礼包的特殊处理
    {
        CDropItem stDropItem1;
        for (int i = 0; i < stGiftItem.Size();)
        {
            CDropItemData stItemData = stGiftItem._astDropItem[i];

                //感恩节抽奖礼包的特殊处理
            if (510 == stItemData._usItemConfigID || 512 == stItemData._usItemConfigID|| 544 == stItemData._usItemConfigID ||

                //幸运转盘抽奖礼包的特殊处理
                578 == stItemData._usItemConfigID || 579 == stItemData._usItemConfigID || 580 == stItemData._usItemConfigID ||
                581 == stItemData._usItemConfigID || 582 == stItemData._usItemConfigID || 583 == stItemData._usItemConfigID ||
                584 == stItemData._usItemConfigID || 
                585 == stItemData._usItemConfigID || 586 == stItemData._usItemConfigID || 587 == stItemData._usItemConfigID ||
                588 == stItemData._usItemConfigID || 589 == stItemData._usItemConfigID || 590 == stItemData._usItemConfigID ||
                591 == stItemData._usItemConfigID)
            {
                if (stItemData._ucItemType == ITEM_TYPE_GIFT)
                {
                    stGiftItem._astDropItem.DelOneItem(i);

                    CDropItem stDropItem2;
                    iRet = GetItemsInGift(uiGid, stItemData._usItemConfigID, stDropItem2);
                    if (RET_OK == iRet)
                    {
                        while (stItemData._iItemCount > 0)
                        {
                            stDropItem1 += stDropItem2;
                            stItemData._iItemCount -= 1;
                        }                    
                    }
                    else
                    {
                        CT_ERROR(("%d, %d, %d", iRet, uiGid, stItemData._usItemConfigID));
                    }

                    continue;
                }
            }

            i++;
        }

        stGiftItem += stDropItem1;
    }

    return 0;
}

int CItemMgr::GetGiftItem(unsigned int uiGid, CGiftDropItemConfig & stItemConfig, CDropItemData & stGiftItem)
{
    int iRet = RET_OK;

    if (IsLevelItem(stItemConfig._stItem._ucItemType, stItemConfig._stItem._usItemId))
    {
        return GetLevelItem(uiGid, stItemConfig._stItem._ucItemType, stItemConfig._stItem._usItemId, stItemConfig._stItem._uiItemCount, stGiftItem);
    }

    if ((stItemConfig._stItem._usItemId == 0) && (stItemConfig._szItemSubType.GetDataLen() > 0))
    {
        iRet = GetDropItemData (0, stItemConfig._stItem._ucItemType, 0, stItemConfig._szItemSubType(),
            COMM_MOD, stItemConfig._stItem._uiItemCount, stGiftItem);
        if (RET_OK == iRet)
        {
            stGiftItem._iItemCount = stItemConfig._stItem._uiItemCount;
        }

        return iRet;
    }

    stGiftItem._ucItemType = stItemConfig._stItem._ucItemType;
    stGiftItem._usItemConfigID = stItemConfig._stItem._usItemId;
    stGiftItem._iItemCount = stItemConfig._stItem._uiItemCount;
    return 0;
}

int CItemMgr::GetLevelItem (unsigned int uiGID, unsigned char ucItemType, unsigned short usItemId, int iDropCount, CDropItemData& stDropItemData)
{
    if (!IsLevelItem(ucItemType, usItemId))
    {
        CT_WARNING(("%d", ucItemType));
        return ERROR_WRONG_ITEM_TYPE;
    }

    int iRet = RET_OK;
    short shLevel = 0;

    iRet = CMainCtrl::m_pstMain->m_stRoleMgr.GetRoleLevel(uiGID, shLevel);
    if (iRet)
    {
        CT_WARNING(("%u, %d", uiGID, iRet));
        return RET_NO_USER;
    }

    CLevelItemConfigData stConfigData;
    iRet = _stLevelItemConfig.GetConfig(shLevel, stConfigData);
    if (iRet)
    {
        CT_ERROR(("%d, %d, %d", uiGID, shLevel, iRet));
        return iRet;
    }

    bool bHasConfigId = false;
    CRateTarget stRateTarget;
    switch (ucItemType)
    {
    case ITEM_TYPE_DROP1:
        {
            iRet = stConfigData._stDropSet1.DropOne(stRateTarget);
            stDropItemData._ucItemType = ITEM_TYPE_SCROLL;
            bHasConfigId = true;
        }
        break;

    case ITEM_TYPE_DROP2:
        {
            iRet = stConfigData._stDropSet2.DropOne(stRateTarget);
            stDropItemData._ucItemType = ITEM_TYPE_MATERIAL;
            bHasConfigId = true;
        }
        break;

    case ITEM_TYPE_DROP3:
        {
            iRet = stConfigData._stDropSet3.DropOne(stRateTarget);
            stDropItemData._ucItemType = ITEM_TYPE_MATERIAL;
            bHasConfigId = true;
        }
        break;

    case ITEM_TYPE_LEVEL_SILVER:
        {
            stDropItemData._ucItemType = ITEM_TYPE_SILVER;
            stDropItemData._iItemCount = (1.0) * stConfigData._iSilverPara * iDropCount / COMM_MOD;
        }
        break;

    case ITEM_TYPE_LEVEL_EXP:
        {
            stDropItemData._ucItemType = ITEM_TYPE_EXP;
            stDropItemData._iItemCount = (1.0) * stConfigData._iExpPara * iDropCount / COMM_MOD / 100 * 100;
        }
        break;

    case ITEM_TYPE_LEVEL_ITEM:
        {
            if (LEVEL_DROP_EQUIP_REFINE == usItemId)
            {
                iRet = stConfigData._stDropSet4.DropOne(stRateTarget);
                stDropItemData._ucItemType = ITEM_TYPE_MATERIAL;
                bHasConfigId = true;
            }
            else
            {
                CT_ERROR(("%d, %d", uiGID, usItemId));
                return ERROR_WRONG_ITEM_TYPE;
            }
        }
        break;

    default:
        iRet = ERROR_WRONG_ITEM_TYPE;
    }

    if (iRet)
    {
        CT_ERROR (("%d, %d, %d, %d", uiGID, shLevel, ucItemType, iDropCount));
        return iRet;
    }

    if (bHasConfigId)
    {
        stDropItemData._iItemCount = iDropCount;
        stDropItemData._usItemConfigID = (unsigned short)stRateTarget._iTargetID;
    }

    CT_TRACE(("%d, %d, %d, %d", uiGID, stDropItemData._ucItemType, stDropItemData._usItemConfigID, stDropItemData._iItemCount));
    return 0;
}

bool CItemMgr::IsLevelItem (unsigned char ucItemType, unsigned short usItemId)
{
    if (ITEM_TYPE_DROP1 == ucItemType
        || ITEM_TYPE_DROP2 == ucItemType
        || ITEM_TYPE_DROP3 == ucItemType
        || ITEM_TYPE_LEVEL_SILVER == ucItemType
        || ITEM_TYPE_LEVEL_EXP == ucItemType)
    {
        return true;
    }

    if (ITEM_TYPE_LEVEL_ITEM == ucItemType
        && (usItemId >= LEVLE_DROP_ITEM_MIN && usItemId <= LEVLE_DROP_ITEM_MAX))
    {
        return true;
    }

    return false;
}

int CItemMgr::DropBonus(int iModule, unsigned int uiGID, unsigned int uiGiftId, int iCount, bool bCheckMutex)
{
    CDropItemData stDropItemData;
    stDropItemData._ucItemType = ITEM_TYPE_GIFT;
    stDropItemData._usItemConfigID = uiGiftId;
    stDropItemData._iItemCount = iCount;
    stDropItemData._iData1 = ADD_GIFT_COEXIST;
    if (bCheckMutex)
    {
        stDropItemData._iData1 = ADD_GIFT_MUTEX;
    }

    CDropItem stDropItem;
    stDropItem.SetOssModule(iModule);
    stDropItem.Drop(stDropItemData);
    int iRet = DropItem(NULL, uiGID, stDropItem, false);
    CHECK_RETURN(iRet);

    return 0;
}

int CItemMgr::IsSameMutexType(unsigned short usGiftId1, unsigned short usGiftId2)
{
    CGiftConfigUnit stConfig1;
    int iRet = _stGiftConfig.GetGiftConfig(usGiftId1, stConfig1);
    CHECK_RETURN(iRet);

    CGiftConfigUnit stConfig2;
    iRet = _stGiftConfig.GetGiftConfig(usGiftId2, stConfig2);
    CHECK_RETURN(iRet);

    return (stConfig1._usMutexType == stConfig2._usMutexType ? true : false);
}

bool CItemMgr::IsGiftValid(unsigned int uiGiftId)
{
    return _stGiftConfig.IsGiftValid(uiGiftId);
}

int CItemMgr::GetGiftConfig(unsigned int usGiftConfigID, CGiftConfigUnit & stConfigData)
{
    return _stGiftConfig.GetGiftConfig(usGiftConfigID, stConfigData);
}

int CItemMgr::GetMaterialConfig(unsigned short usMaterialConfigID, CMaterialConfigData& stConfigData)
{
    return _stMaterialConfig.GetConfig(usMaterialConfigID, stConfigData);
}

int CItemMgr::GetMaterialConfig(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey,
                                CMaterialConfigData& stConfigData)
{
    return _stMaterialConfig.GetConfig(szKey, stConfigData);
}

int CItemMgr::GetCrystalConfig(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey,
                               CCrystalConfigData& stConfigData)
{
    return _stCrystalConfig.GetConfig(szKey, stConfigData);
}

int CItemMgr::GetCrystalConfig(unsigned short usCrystalConfigID, CCrystalConfigData& stConfigData)
{
    return _stCrystalConfig.GetConfig(usCrystalConfigID, stConfigData);
}

int CItemMgr::GetJewelConfig(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey,
                             CJewlConfigData& stConfigData)
{
    return _stJewelConfig.GetConfig(szKey, stConfigData);
}

int CItemMgr::GetJewelConfig(unsigned short usConfigID, CJewlConfigData& stConfigData)
{
    return _stJewelConfig.GetConfig(usConfigID, stConfigData);
}

int CItemMgr::GetFruitConfig(unsigned short usFruitId, CFruitUnitConfig & stConfigData)
{
    return _stFruitCfg.GetFruitConfig(usFruitId, stConfigData);
}

int CItemMgr::DropCrystalFrag (unsigned short usCrystalId, CDropItemData & stDropItemData)
{
    CCrystalConfigData stCrystalCfg;
    int iRet = _stCrystalConfig.GetConfig(usCrystalId, stCrystalCfg);
    CHECK_RETURN(iRet);

    if (stCrystalCfg._usDecompFragMax < stCrystalCfg._usDecompFragMin)
    {
        return ERROR_CRYSTAL_CONFIG;
    }

    stDropItemData._ucItemType = ITEM_TYPE_CRYSTAL_FRAG;
    stDropItemData._usItemConfigID = _stCrystalConfig._stCrystalFragCfg._usCrystaFraglId;
    stDropItemData._iItemCount = stCrystalCfg._usDecompFragMin +  CT_RANDOM_NUM->GetRandHaveMax(stCrystalCfg._usDecompFragMax - stCrystalCfg._usDecompFragMin);
    return 0;
}

int CItemMgr::AutoSell(CDropItem& stDropItem)
{
    int iRet = 0;
    CDropItem stAfterSellItem;
    int iItemPrice = 0;

    for (int i = 0; i < stDropItem._astDropItem.Size();)
    {
        CDropItemData& stDropItemData = stDropItem._astDropItem[i];
        iItemPrice = 0;

        if (ITEM_TYPE_EQUIP == stDropItemData._ucItemType)
        {
            CEquipConfigData stConfigData;
            iRet = CMainCtrl::m_pstMain->m_stEquipMgr.GetConfigData(stDropItemData._usItemConfigID, stConfigData);
            if (iRet != RET_OK || !stConfigData._bCanAutoSellInIns)
            {
                stAfterSellItem.Drop(stDropItemData);
                ++i;
                continue;
            }
        }
        else if (ITEM_TYPE_SCROLL == stDropItemData._ucItemType)
        {
            CScrollConfigData stConfigData;
            iRet = _stScrollConfig.GetConfig(stDropItemData._usItemConfigID, stConfigData);
            if (iRet != RET_OK || !stConfigData._bCanAutoSellInIns)
            {
                stAfterSellItem.Drop(stDropItemData);
                ++i;
                continue;
            }
        }
        else if (ITEM_TYPE_MATERIAL == stDropItemData._ucItemType)
        {
            CMaterialConfigData stConfigData;
            iRet = _stMaterialConfig.GetConfig(stDropItemData._usItemConfigID, stConfigData);
            if (iRet != RET_OK || !stConfigData._bCanAutoSellInIns)
            {
                stAfterSellItem.Drop(stDropItemData);
                ++i;
                continue;
            }
        }
        else
        {
            stAfterSellItem.Drop(stDropItemData);
            ++i;
            continue;
        }

        iRet = GetItemPrice(stDropItemData._ucItemType, stDropItemData._usItemConfigID, stDropItemData._iItemCount, ITEM_SELL_PRICE, iItemPrice);
        if (RET_OK != iRet)
        {
            CT_WARNING(("auto sell error ret %d, %d, %d", iRet, stDropItemData._ucItemType, stDropItemData._usItemConfigID));
            stAfterSellItem.Drop(stDropItemData);
            ++i;
            continue;
        }

        stAfterSellItem.DropSilver(iItemPrice);
        stDropItem._astDropItem.ShiftDelOneItem(i);
    }

    stDropItem.Clear();
    stDropItem = stAfterSellItem;

    return 0;
}

void CItemMgr::NotifyDropItem(unsigned int uiGID, CDropItemData & stDropItemData)
{
    CDropItem stDropItem;
    stDropItem.Drop(stDropItemData);

    NotifyDropItem(uiGID, stDropItem);
}

void CItemMgr::NotifyDropItem(unsigned int uiGID, CDropItem& stDropItem)
{
    int iRet = 0;
    CEnpNetHead stEnpHead;
    iRet = CMainCtrl::m_pstMain->m_stPlayerOnline.GetOnlinePlayerHandle(uiGID, stEnpHead);
    if (iRet)
    {
        return;
    }

    MsgDropNewItem stAns;
    stDropItem.GetPBMsg(uiGID, *(stAns.mutable_drop()));

    PBMsgHead stMsgHead;
    stMsgHead.set_time(CMainCtrl::m_pstMain->m_iNow);
    stMsgHead.set_msgact(0);
    stMsgHead.set_msgret(RET_OK);
    stMsgHead.set_msgid(ID_MAIN_NOTIFY_DROP_ITEM);
    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);

    CTBuffer& stEncodeBuf = CMainCtrl::m_pstMain->m_stEncodeBuf;
    stEncodeBuf.Clear();
    CMainCtrl::m_pstMain->EncodePBMsg(stMsgHead, stEncodeBuf);
    CMainCtrl::m_pstMain->EncodePBMsg(stAns, stEncodeBuf);

    CMainCtrl::m_pstMain->NotifyOnlineUser(uiGID, stEncodeBuf);
}

int CItemMgr::SubGold (unsigned int uiGID, int iModule, int & iNowGold, int iToSubGold)
{
    CMainCtrl * pstMainCtrl = CMainCtrl::m_pstMain;
    int iRet = CPlayerBaseInfo::SubGold(iNowGold, iToSubGold);
    CHECK_RETURN(iRet);

    if ((OSS_MONEY_MODULE_28 != iModule) && (OSS_MONEY_MODULE_41 != iModule))
    {
        //消费返利
        CDropItem stDropItem;
        CDropItemData stDropItemData;
        stDropItemData._ucItemType = ITEM_TYPE_USE_GOLD_POINT;
        stDropItemData._iItemCount = iToSubGold;
        stDropItem.Drop(stDropItemData);
        DropItem(NULL, uiGID, stDropItem, false);

        CMainCtrl::m_pstMain->m_stActMgr.TriggerSubMoney(uiGID, iToSubGold, 0);
    }

    pstMainCtrl->m_stOssMgr.TriggerMoney(uiGID, OSS_MONEY_SUB_GOLD, iModule, iToSubGold);

    return 0;
}

int CItemMgr::SubGold (CPlayerBaseInfo & stPlayerBaseInfo, int iModule, int iToSubGold)
{
    return SubGold(stPlayerBaseInfo._uiGID, iModule, stPlayerBaseInfo._iGold, iToSubGold);
}

int CItemMgr::SubSilver(CPlayerBaseInfo& stPlayerBaseInfo, int iToSubSilver, int iModule)
{
    int iRet = CPlayerBaseInfo::SubSilver(stPlayerBaseInfo._i64Silver, iToSubSilver);
    if (iRet)
    {
        return iRet;
    }

    if ((OSS_MONEY_MODULE_28 != iModule) && (OSS_MONEY_MODULE_41 != iModule) && (OSS_MONEY_MODULE_59 != iModule))
    {
        CMainCtrl::m_pstMain->m_stActMgr.TriggerSubMoney(stPlayerBaseInfo._uiGID, 0, iToSubSilver);
    }

    return 0;
}

int CItemMgr::GetIconName (unsigned char ucItemType, unsigned short usItemCfgID, CTSizeString<MAX_ICON_NAME_LEN> & stIconName)
{
    stIconName.Set("");

    int iRet = RET_OK;
    switch (ucItemType)
    {
    case ITEM_TYPE_MATERIAL:
        {
            CMaterialConfigData stConfigData;
            iRet = _stMaterialConfig.GetConfig(usItemCfgID, stConfigData);
            CHECK_RETURN(iRet);

            stIconName = stConfigData._szIconName;
        }
        break;

    case ITEM_TYPE_GIFT:
        {
            CGiftConfigUnit stConfigData;
            iRet = GetGiftConfig(usItemCfgID, stConfigData);
            CHECK_RETURN(iRet);

            stIconName = stConfigData._szIconName;
        }
        break;

    case ITEM_TYPE_CRYSTAL:
        {
            CCrystalConfigData stConfigData;
            iRet = GetCrystalConfig(usItemCfgID, stConfigData);
            CHECK_RETURN(iRet);

            stIconName = stConfigData._szIconName;
        }
        break;

    case ITEM_TYPE_MOUNT:
        {
            iRet = CMainCtrl::m_pstMain->m_stMountMgr.GetMountIcon (usItemCfgID, stIconName);
            CHECK_RETURN(iRet);
        }
        break;

    default:
        return ERROR_WRONG_ITEM_TYPE;
    }

    return RET_OK;
}
